/*     */ package com.zimbra.cs.index.query;
/*     */ 
/*     */ import com.zimbra.common.service.ServiceException;
/*     */ import com.zimbra.cs.index.LuceneQueryOperation;
/*     */ import com.zimbra.cs.index.QueryOperation;
/*     */ import com.zimbra.cs.index.analysis.FieldTokenStream;
/*     */ import com.zimbra.cs.mailbox.Mailbox;
/*     */ import java.util.regex.Matcher;
/*     */ import java.util.regex.Pattern;
/*     */ import org.apache.lucene.index.Term;
/*     */ import org.apache.lucene.search.TermQuery;
/*     */ import org.apache.lucene.search.TermRangeQuery;
/*     */ import org.apache.lucene.util.NumericUtils;
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ public final class FieldQuery
/*     */   extends TextQuery
/*     */ {
/*  41 */   private static final Pattern NUMERIC_QUERY_REGEX = Pattern.compile("(<|>|<=|>=)?-?\\d+");
/*     */   
/*     */   private FieldQuery(String name, String value) {
/*  44 */     super(new FieldTokenStream(name, value), "l.field", String.format("%s:%s", new Object[] { name, value == null ? "" : value }));
/*     */   }
/*     */   
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */ 
/*     */   public static Query create(Mailbox mbox, String name, String value)
/*     */     throws ServiceException
/*     */   {
/*  60 */     if (NUMERIC_QUERY_REGEX.matcher(value).matches()) {
/*     */       try {
/*  62 */         return new NumericRangeFieldQuery(name, value);
/*     */       }
/*     */       catch (NumberFormatException e) {}
/*     */     }
/*  66 */     return new FieldQuery(name, value);
/*     */   }
/*     */   
/*     */   private static final class NumericRangeFieldQuery extends Query { private final String name;
/*     */     
/*  71 */     private static enum Range { EQ(""),  GT(">"),  GT_EQ(">="),  LT("<"),  LT_EQ("<=");
/*     */       
/*     */       private final String sign;
/*     */       
/*     */       private Range(String sign) {
/*  76 */         this.sign = sign;
/*     */       }
/*     */       
/*     */       public String toString()
/*     */       {
/*  81 */         return this.sign;
/*     */       }
/*     */     }
/*     */     
/*     */     private final int number;
/*     */     private final Range range;
/*     */     NumericRangeFieldQuery(String name, String value)
/*     */       throws NumberFormatException
/*     */     {
/*  90 */       this.name = name.toLowerCase();
/*  91 */       if (value.startsWith("<")) {
/*  92 */         if (value.startsWith("=", 1)) {
/*  93 */           this.range = Range.LT_EQ;
/*  94 */           this.number = Integer.parseInt(value.substring(2));
/*     */         } else {
/*  96 */           this.range = Range.LT;
/*  97 */           this.number = Integer.parseInt(value.substring(1));
/*     */         }
/*  99 */       } else if (value.startsWith(">")) {
/* 100 */         if (value.startsWith("=", 1)) {
/* 101 */           this.range = Range.GT_EQ;
/* 102 */           this.number = Integer.parseInt(value.substring(2));
/*     */         } else {
/* 104 */           this.range = Range.GT;
/* 105 */           this.number = Integer.parseInt(value.substring(1));
/*     */         }
/*     */       } else {
/* 108 */         this.range = Range.EQ;
/* 109 */         this.number = Integer.parseInt(value);
/*     */       }
/*     */     }
/*     */     
/*     */     public boolean hasTextOperation()
/*     */     {
/* 115 */       return true;
/*     */     }
/*     */     
/*     */     public QueryOperation compile(Mailbox mbox, boolean bool)
/*     */     {
/* 120 */       org.apache.lucene.search.Query query = null;
/* 121 */       switch (FieldQuery.1.$SwitchMap$com$zimbra$cs$index$query$FieldQuery$NumericRangeFieldQuery$Range[this.range.ordinal()]) {
/*     */       case 1: 
/* 123 */         query = new TermQuery(new Term("l.field", this.name + "#:" + NumericUtils.intToPrefixCoded(this.number)));
/*     */         
/* 125 */         break;
/*     */       case 2: 
/* 127 */         query = new TermRangeQuery("l.field", this.name + "#:" + NumericUtils.intToPrefixCoded(this.number), this.name + "#:" + NumericUtils.intToPrefixCoded(Integer.MAX_VALUE), false, true);
/*     */         
/*     */ 
/*     */ 
/* 131 */         break;
/*     */       case 3: 
/* 133 */         query = new TermRangeQuery("l.field", this.name + "#:" + NumericUtils.intToPrefixCoded(this.number), this.name + "#:" + NumericUtils.intToPrefixCoded(Integer.MAX_VALUE), true, true);
/*     */         
/*     */ 
/*     */ 
/* 137 */         break;
/*     */       case 4: 
/* 139 */         query = new TermRangeQuery("l.field", this.name + "#:" + NumericUtils.intToPrefixCoded(Integer.MIN_VALUE), this.name + "#:" + NumericUtils.intToPrefixCoded(this.number), true, false);
/*     */         
/*     */ 
/*     */ 
/* 143 */         break;
/*     */       case 5: 
/* 145 */         query = new TermRangeQuery("l.field", this.name + "#:" + NumericUtils.intToPrefixCoded(Integer.MIN_VALUE), this.name + "#:" + NumericUtils.intToPrefixCoded(this.number), true, true);
/*     */         
/*     */ 
/*     */ 
/* 149 */         break;
/*     */       default: 
/* 151 */         if (!$assertionsDisabled) throw new AssertionError();
/*     */         break;
/*     */       }
/* 154 */       LuceneQueryOperation op = new LuceneQueryOperation();
/* 155 */       op.addClause(toQueryString("l.field", this.range.toString() + this.number), query, evalBool(bool));
/* 156 */       return op;
/*     */     }
/*     */     
/*     */     void dump(StringBuilder out)
/*     */     {
/* 161 */       out.append('#');
/* 162 */       out.append(this.name);
/* 163 */       out.append("#:");
/* 164 */       out.append(this.range);
/* 165 */       out.append(this.number);
/*     */     }
/*     */     
/*     */     void sanitizedDump(StringBuilder out)
/*     */     {
/* 170 */       out.append('#');
/* 171 */       out.append(this.name);
/* 172 */       out.append("#:");
/* 173 */       out.append(this.range);
/* 174 */       out.append("$NUM");
/*     */     }
/*     */   }
/*     */ }


/* Location:              /home/mint/zimbrastore.jar!/com/zimbra/cs/index/query/FieldQuery.class
 * Java compiler version: 7 (51.0)
 * JD-Core Version:       0.7.1
 */